﻿#include "animationParseHEX.h"

AnimationParseHEX::AnimationParseHEX()
{

}

// 62 00 00 3A 00 08 01 00 01 01 00 64 B7 9A AA 55
// 62 00 00 3A 00 06 01 01 02 01 B7 9A AA 55
// 62 00 00 3A 00 06 01 02 01 01 B7 9A AA 55

bool AnimationParseHEX::startParsing(AnimationState *state, QStringList instruct){



    if(instruct[0] == "推盖"){
        parsingHatPush(state, instruct);
        return true;
    }if(instruct[0] == "盖盖"){
        parsingHatPress(state, instruct);
        return true;
    }
    if(instruct.size() < 8){
        qDebug() << "ERROR: [Class]AnimationParseHEX [Func]startParsing - instruct out-of-range" << instruct.size();
        return false;
    }

    if(instruct[3]== "3D" && instruct[6] == "03"){      // 电机指令
        parsingMotor(state, instruct);
        return true;
    }

    qDebug() << "接收到下位机解析指令，开始执行解析" << instruct;
    __LOG_INFO("接收到下位机解析指令，开始执行解析:" + instruct.join(" "));
    if(lastInstruction == instruct.join(" ")){
        __LOG_INFO("指令发送重复，跳过解析");
        return false;
    }lastInstruction = instruct.join(" ");

    if(instruct[3] == "3A"){        // 实验单独指令数据-3A 不解析 (主要为了区分重复指令)
        return true;
    }



    if(instruct[7] == "01"){    // 注射器
        parsingSyringe(state, instruct);
    }else if(instruct[7] == "02"){    // 阀门
        parsingValve(state, instruct);
    }else if(instruct[7] == "03"){    // 转盘
        parsingWheel(state, instruct);
    }else if(instruct[7] == "04"){    // 推盖
        parsingHatPush(state, instruct);
        QTimer::singleShot(1500, [this, state, instruct](){     // 推盖和盖盖指令在这块合并了
            parsingHatPress(state, instruct);
        });
    }else if(instruct[7] == "05"){
        parsingHatPress(state, instruct);
    }
//    if(instruct[7] == "05"){
//        parsingHat(state, instruct);
//    }

    return true;
}



bool AnimationParseHEX::parsingSyringe(AnimationState *state, QStringList instruct){


    if(instruct.size() < 15){
        qDebug() << "ERROR: [Class]AnimationParseHEX [Func]startParsing - instruct out-of-range" << instruct.size();
        return false;
    }

    // 08-编号 09-动作01打液02吸液 10 11-步数/s 12 13 14 15-总步数

    int idx = instruct[8].toInt();

    int direct = instruct[9].toInt()*2-3;               // 方向

    double stepSpeed = CommonHelper::commonInstance()->SixteenToTen(instruct[10]+instruct[11]).toInt();
    double stepNum = CommonHelper::commonInstance()->SixteenToTen(instruct[12]+instruct[13]+instruct[14]+instruct[15]).toInt();

    qDebug() << "接收注射器指令" << "[编号]" << idx << "[方向]" << direct << "[速度]" << stepSpeed << "总步数" << stepNum;

    double mmSpeed = SyringeCalculator::calStep2MM(idx, stepSpeed);
    double pushHeight = SyringeCalculator::calStep2MM(idx, stepNum);
    double syringeHeight = SyringeCalculator::getSyringeHeightMM(idx);

    qDebug() << "注射器速度" << mmSpeed << "  推的高度" << pushHeight << "  注射器的高度" << syringeHeight;

    state->changeSyringeSpeedTarget(idx, mmSpeed);      // [1] 变更标签上的速度

    qDebug() << "速度百分比" << mmSpeed / syringeHeight;
    state->changeSyringe_dp(idx, -direct*(pushHeight / syringeHeight), mmSpeed / syringeHeight);        // [2] 变更注射器变化

    double diameter = SyringeCalculator::getSyringeDiameterMM(idx);
    double speed_ml_s = 3.14159 * (diameter*diameter) * mmSpeed / 4000;  // 毫升每秒

    qDebug() << "溶液的流速" << speed_ml_s << "ml/s";

    state->_pipeGraph->changeSyringeSpeed(idx, speed_ml_s);     // [3] 变更管道流速


    double pushVolumn = pow(diameter/2, 2) * pushHeight;        // 推的体积（单位可能有点问题，但是不管了……）

    // 单独处理一下溶靶瓶…………
    if(state->_valveState.size() > 3){
        double bottleHeight = SyringeCalculator::getSyringeHeightMM(0);
        double bottlePushHeight = pushVolumn / pow(SyringeCalculator::getSyringeDiameterMM(0)/2, 2);
        double bottlePushPercent = bottlePushHeight / bottleHeight;
        double bottleMMSpeed = mmSpeed * (diameter*diameter) / pow(SyringeCalculator::getSyringeDiameterMM(0), 2);
        double bottlePercentSpeed = bottleMMSpeed / bottleHeight;
        if(idx == 1){
            if(state->_valveState[0] && state->_valveState[2]){
                state->changeSyringe_dp(0, direct*bottlePushPercent, bottlePercentSpeed);
            }
        }else if(idx == 2){
            if(state->_valveState[1] && state->_valveState[2]){
                state->changeSyringe_dp(0, direct*bottlePushPercent, bottlePercentSpeed);
            }
        }else if(idx == 3){
            if(state->_valveState[3]){
                state->changeSyringe_dp(0, direct*bottlePushPercent, bottlePercentSpeed);
            }
        }
    }

    return true;
}
bool AnimationParseHEX::parsingValve(AnimationState *state, QStringList instruct){

    if(instruct.size() < 11){
        qDebug() << "ERROR: [Class]AnimationParseHEX [Func]startParsing - instruct out-of-range" << instruct.size();
        return false;
    }
    QString valveState = CommonHelper::commonInstance()->SixteenToTwo(instruct[8]+instruct[9]+instruct[10]);
    // 补位
    int tsize = 24-valveState.size();
    for(int i = 0; i < tsize; i ++){
        valveState = "0"+valveState;
    }


    for(int i = 0; i < valveState.size(); i ++){

        int idx = i/8*8 + (7-i%8);

        int istate = QString(valveState[i]).toInt();
        if(istate == 0){
            state->changeValve(idx, true);
        }else if(istate == 1){
            state->changeValve(idx, false);
        }
    }
//    state->changeValve(idx, status==1);
    return true;
}

bool AnimationParseHEX::parsingWheel(AnimationState *state, QStringList instruct){

    if(instruct.size() < 10){
        qDebug() << "ERROR: [Class]AnimationParseHEX [Func]startParsing - instruct out-of-range" << instruct.size();
        return false;
    }

    // 08-转动位置 09-盖盖(0不盖 1盖)
    int posi = instruct[8].toInt() -  1;        // 我这边的序号是0~8
    qDebug() << "解析转盘转动位置" << posi;
//    int addHat = instruct[9].toInt();
    state->turnWheel_posi(posi, 1);
//    if(addHat == 1){
//        QTimer::singleShot(1000, [state]{
//            state->addHat();
//        });
//    }
    return true;
}

bool AnimationParseHEX::parsingHatPush(AnimationState *state, QStringList instruct){
    state->pushHat();
    return true;
}
bool AnimationParseHEX::parsingHatPress(AnimationState *state, QStringList instruct){
    state->pressHat();
    return true;
}

bool AnimationParseHEX::parsingMotor(AnimationState *state, QStringList instruct){
    qDebug() << "解析电机速度";
    if(instruct.size() < 19){
        __LOG_ERROR("Instruct-OutOfRange");
        __LOG_FEND(false);
        return false;
    }

    // 注射器速度 (步数/s)
    double speedStep1 = CommonHelper::commonInstance()->SixteenToTen(instruct[7]+instruct[8]).toInt();
    double speedStep2 = CommonHelper::commonInstance()->SixteenToTen(instruct[9]+instruct[10]).toInt();
    double speedStep3 = CommonHelper::commonInstance()->SixteenToTen(instruct[11]+instruct[12]).toInt();
    double speedStep4 = CommonHelper::commonInstance()->SixteenToTen(instruct[13]+instruct[14]).toInt();
    double speedStep5 = CommonHelper::commonInstance()->SixteenToTen(instruct[15]+instruct[16]).toInt();
    double speedStep6 = CommonHelper::commonInstance()->SixteenToTen(instruct[17]+instruct[18]).toInt();


    double speedMM1 = SyringeCalculator::calStep2MM(1, speedStep1);
    double speedMM2 = SyringeCalculator::calStep2MM(2, speedStep2);
    double speedMM3 = SyringeCalculator::calStep2MM(3, speedStep3);
    double speedMM4 = SyringeCalculator::calStep2MM(4, speedStep4);
    double speedMM5 = SyringeCalculator::calStep2MM(5, speedStep5);
    double speedMM6 = SyringeCalculator::calStep2MM(6, speedStep6);

    state->changeSyringeSpeedRealTime(1, speedMM1);
    state->changeSyringeSpeedRealTime(2, speedMM2);
    state->changeSyringeSpeedRealTime(3, speedMM3);
    state->changeSyringeSpeedRealTime(4, speedMM4);
    state->changeSyringeSpeedRealTime(5, speedMM5);
    state->changeSyringeSpeedRealTime(6, speedMM6);


    __LOG_FEND(true);
    return true;
}


